package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.income.expense.detail.IncomeExpenseDetailQueryArgument;
import com.opennews.openplatform.dto.journey.income.expense.detail.CreateJourneyIncomeExpenseDetailDto;
import com.opennews.openplatform.dto.journey.income.expense.detail.UpdateJourneyIncomeExpenseDetailDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto;
import com.opennews.openplatform.myspringbootcore.security.MyClaims;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.JourneyIncomeExpenseDetailService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/journey-income-expense-detail")
public class JourneyIncomeExpenseDetailController {
    private final JourneyIncomeExpenseDetailService journeyIncomeExpenseDetailService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/query-existing-distinct-years")
    public ResponseEntity<ApiResponse<List<Integer>>> queryExistingDistinctYears() {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        List<Integer> result = journeyIncomeExpenseDetailService.queryExistingDistinctYears(accountGroupId);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-list-by-journey")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryListByJourney(
            @RequestParam String journeyId,
            @RequestParam String keyword,
            @RequestParam int pageSize,
            @RequestParam int pageIndex) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;

        // Prepares the query argument.
        IncomeExpenseDetailQueryArgument queryArgument = new IncomeExpenseDetailQueryArgument();
        queryArgument.accountGroupId = accountGroupId;
        queryArgument.keyword = keyword;
        queryArgument.pageSize = pageSize;
        queryArgument.pageIndex = pageIndex;

        List<Map<String, Object>> result = journeyIncomeExpenseDetailService.queryListByJourney(journeyId, queryArgument);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-details")
    public ResponseEntity<ApiResponse<Map<String, Object>>> queryDetails(@RequestParam String id) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        Map<String, Object> result = journeyIncomeExpenseDetailService.queryDetails(accountGroupId, id);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-coordinates")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryCoordinates(@RequestParam String year) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        List<Map<String, Object>> result = journeyIncomeExpenseDetailService.queryCoordinates(accountGroupId, year);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse<Object>> add(@Valid @RequestBody CreateJourneyIncomeExpenseDetailDto data) {
        MyClaims claims = requestIdentityExtractor.getClaims();
        journeyIncomeExpenseDetailService.add(claims.accountGroupId, claims.username, data);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse<Object>> update(@Valid @RequestBody UpdateJourneyIncomeExpenseDetailDto data) {
        MyClaims claims = requestIdentityExtractor.getClaims();
        journeyIncomeExpenseDetailService.update(claims.accountGroupId, claims.username, data);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/delete")
    public ResponseEntity<ApiResponse<Object>> delete(@Valid @RequestBody IdentityDto data) {
        String id = data.getId();
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        journeyIncomeExpenseDetailService.delete(accountGroupId, List.of(id));

        return httpResponseHelper.getSuccessResponse();
    }
}
